package com.itrip.listener;

import com.itrip.service.ItripHotelTempStoreService;
import com.itrip.service.OrderService;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {


    @Resource
    private ItripHotelTempStoreService itripHotelTempStoreService;

    @Resource
    private OrderService orderService;

    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        //获取失效的key
        String expiredKey = message.toString();

        //截取判断标志
        String[] split1 = expiredKey.split(":");

        //进行一个判断这个失效的key是不是关于订单
        if(!"order".equals(split1[0])){  //不是关于订单直接返回
            return;
        }

        //订单失效操作
        String[] split = expiredKey.split(",");
        //截取字符串获取订单号
        String a1=split[0];
        String orderNo=a1.substring(a1.indexOf(":")+1);
        System.out.println(orderNo);
        //截取字符串获取酒店Id
        String hotelId=split[1];
        System.out.println(hotelId);
        //截取字符串获房间Id
        String roomId=split[2];
        System.out.println(roomId);
        //截取字符串获取消耗房间数
        String count=split[3];
        int count1=Integer.parseInt(count);
        System.out.println(count);


        //恢复库存
        int hotelId1 = Integer.parseInt(hotelId);
        long roomId1 =  Long.parseLong(roomId);
        int count2 = itripHotelTempStoreService.resumeTempStore(hotelId1,roomId1,count1);

        //设置订单失效
        int count3 =orderService.setOrderStatus(orderNo);

        //删除订单？？？
        System.out.println("失效key："+expiredKey);
    }
}